package com.xmall.controller.wx;

import com.xmall.bean.bo.CommentBO;
import com.xmall.bean.table.OrderGoods;
import com.xmall.bean.vo.BaseRespVo;
import com.xmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 谭涛
 */
@RequestMapping("wx/order")
@RestController("wxOrderController")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     *添加评论之后，同时还要更新order_goods 表中的 comment 值
     *以及 order 表中的 comments 数量
     */
    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody CommentBO commentBO){
       orderService.addComment(commentBO);
        return BaseRespVo.ok();
    }

    @RequestMapping("list")
    public BaseRespVo list(@RequestParam Map<String , String> params){
        Map<String , Object> result = orderService.queryList(params);
        return BaseRespVo.ok(result);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId){
        Map<String , Object> result = orderService.queryDetail(orderId);
        return BaseRespVo.ok(result);
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map<String,Integer> params){
        orderService.cancelOrder(params.get("orderId"));
        return BaseRespVo.ok();
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map<String,Integer> params){
        orderService.refund(params.get("orderId"));
        return BaseRespVo.ok();
    }

    /**
     * 同时删除order，order_goods,commment
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map<String,Integer> params){
        orderService.delete(params);
        return BaseRespVo.ok();
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map<String,Integer> params){
        orderService.confirmOrder(params.get("orderId"));
        return BaseRespVo.ok();
    }

    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId,Integer goodsId){
        OrderGoods orderGoods = orderService.queryGoods(orderId,goodsId);
        return BaseRespVo.ok(orderGoods);
    }

    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody Map<String, String> params){
        // TODO: 2020/12/10 test
        Map<String, Integer> result = orderService.submit(params);
        return BaseRespVo.ok(result);
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map<String, Object> params){
        //todo test
        Object orderId = params.get("orderId");
        Integer id = null;
        if (orderId instanceof String){
            id = Integer.parseInt((String) orderId);
        }else if (orderId instanceof Integer){
            id = (Integer)orderId;
        }
        orderService.prepay(id);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(724);
        baseRespVo.setErrmsg("订单不能支付");
        return baseRespVo;
    }
}
